<?php

namespace app\store\model\presalesCustomer;

class Source extends \app\common\model\presalesCustomer\Source
{
    /**
     * 检索查询条件
     * @param array $param
     * @return array
     */
    private function getFilter(array $param = []): array
    {
        // 默认查询条件
        $param = $this->setQueryDefaultValue($param, [
            'search' => '',
            'status' => -1,
            'create_time' => [],
        ]);
        // 检索查询条件
        $filter = [];
        !empty($param['search']) && $filter[] = ['source_name', 'like', "%{$param['search']}%"];
        $param['status'] > -1 && $filter[] = ['status', '=', $param['status']];

        if (!empty($param['create_time'])) {
            $times = between_time($param['create_time']);
            $filter[] = ['create_time', '>=', $times['start_time']];
            $filter[] = ['create_time', '<', $times['end_time'] + 86400];
        }
        return $filter;
    }

    /**
     * 获取列表记录
     * @param array $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $param = [], array $store = []): \think\Paginator
    {
        // 检索查询条件
        $filter = $this->getFilter($param);
        // 查询列表数据
        return $this->where($filter)
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->paginate(15);
    }

    public function all(array $param = [], array $store = [])
    {
        // 检索查询条件
        $filter = $this->getFilter($param);

        // 查询列表数据
        return $this->where($filter)
            ->where('is_delete', '=', 0)
            ->where('status', '=', 0)
            ->order(['create_time' => 'desc'])
            ->select();
    }


    public function findSourceId($name)
    {
        $info = $this->where(['source_name' => $name, 'status' => 0, 'is_delete' => 0])->find();
        if (!$info) {
            return 0;
        }
        return $info->pcs_id;
    }
}
